<?php
	include_once("dbi.php");

	$user_logged = isset($_SESSION[''.SECURITY_SESSION.'']['username']) ? $_SESSION[''.SECURITY_SESSION.'']['username'] : FALSE;
	
	if ($user_logged){
		$start_date = $_POST['start_date'];
		$shour = $_POST['shour'];
		$smin = $_POST['smin'];
		$duration = $_POST['duration'];
		
		$connection = array('HOST' => ''.HOST_MYSQL.'', 'USER' => ''.USER_MYSQL.'', 'PASS' => ''.PASS_MYSQL.'', 'INVENTORY_DB' => ''.DB_INVENTORY.'', 'SCHEDULER_DB' => ''.DB_SCHEDULER.'');

		$m = (!$m) ? date("m",mktime()) : "$m";
		$y = (!$y) ? date("Y",mktime()) : "$y";

		$now_date = date("Y-m-d H:i:s", mktime());

		if (isset($start_date)) {
			$timestamp = strtotime("".$start_date." ".$shour.":".$smin.":00");
			
			//Data validation to advise user to select a date after now
			if ($timestamp < time())
				set_message('Select a present or future date!','alert',1);
		}

		$now = time();
		$start_time = $start_date . " " . $shour . ":" . $smin . ":" . "00";
		$asked_date = strtotime($start_time);

		if(($_REQUEST['reserve']=="true") && $_REQUEST['start']) {
			$error = 0;

			$book_nodes = array();
			$book_nodes = $_REQUEST['book_nodes'];
			$book_specs = $_REQUEST['book_specs'];
			$book_begin_time = $_REQUEST['start'];
			$book_end_time = $_REQUEST['end'];

			$numofnodes = count($book_nodes);
			$numofspecs = count($book_specs);
				
			for ($k=0; $k<$numofnodes; $k++){
				if ((isNodeAvailable_id($book_begin_time, $book_end_time, $book_nodes[$k], $connection) == 'true')){
					//create the reservation
					$data = array(
							'user' => $user_logged,
							'begin_time' => $book_begin_time,
							'end_time' => $book_end_time,
							'node' => $book_nodes[$k]
							);
						
					if(!CreateNodeReservation($data, $connection)) $error++;
				}
			}

			for ($h=0; $h<$numofspecs; $h++){
				//create the reservation
				$data = array(
						'user' => $user_logged,
						'begin_time' => $book_begin_time,
						'end_time' => $book_end_time,
						'channel' => $book_specs[$h]
						);
					
				if(!CreateChannelReservation($data, $connection)) $error++;
			}

			if($error > 0)
				set_message('Error in Reservation, try again','error',1);
			else
				set_message('Your Nodes and your Frequencies have been booked! Check My Reservations!','success',1);
		}
	}else{
		set_message('You do not have a slice! Please contact LABORA for a slice creation!','error',1);
	}
?>


<body>
	<div id='text'><a class='title'>Reservation » <?php echo $user_logged; ?></a></div>
	<table id='reservation_table'> 
		<tr>
			<td class='centralize' colspan='2'>
				 Current Server Time: <?php echo $now_date; ?>
			</td>
		</tr>
		<tr>
			<td>
				<?php mk_drawCalendar($_REQUEST['m'],$_REQUEST['y']); ?>
			</td>
			<td>
				<form name="f" action="?page=reservation&m=<?php echo $m; ?>&y=<?php echo $y; ?>" method="POST">
					<table height="153px">
						<tr>
							<td colspan="2" bgcolor="#000000">
								<font color="#FFFFFF"><b>Select Start Time and Duration</b></font>
							</td>
						</tr>
						<tr>
							<td>
								<b>Start Date: </b>
							</td>
							<td>
								<input type="text" name="start_date" size="10" value="<?php echo $start_date; ?>" readonly> <font size="2">yyyy-mm-dd</font>
							</td>
						</tr>	
						<tr>
							<td>
								<b>Start Time: </b>
							</td>
							<td>
								<select name="shour">
									<?php
									  for($i=0;$i<24;$i++){

										$s_hour=($i<10) ? $i="0" . $i : $i;
										
										if($s_hour == $shour)
											echo '<option value="' . $s_hour . '" selected>' . $s_hour . '</option>';
										else
											echo '<option value="' . $s_hour . '">' . $s_hour . '</option>';
									  }
									?>
								</select>
								<font size="2">hh</font>
								
								<select name="smin">
									<?php
										if($smin == '30')
											echo '<option value="00">00</option>
											<option value="30" selected>30</option>';
										else
											echo '<option value="00" selected>00</option>
											<option value="30">30</option>';
									?>
								</select>
								<font size="2"> min</font>
							</td>
						</tr>
						<tr>
							<td>
								<b>Duration: </b>
							</td>
							<td>
								<select name="duration">
									<?php
									  for($i=0.5;$i<4.5;$i=$i+0.5){
										if($duration == $i)
											echo '<option value="' . $i . '" selected>' . $i . '</option>';
										else
											echo '<option value="' . $i . '">' . $i . '</option>';
									  }
									?>
								</select>
								<font size="2"> hours</font>
							</td>
						</tr>
						<tr>
							<td colspan="2" align="center" bgcolor="#CCCCCC">
								<input type="submit" value="Check Available Nodes" />
							</td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
	
	<?php
		if ($now < $asked_date) {
			$tok2 = strtok($duration, ".");
			$th=$tok2;
			
			while ($tok2 !== false){    
				$tok2 = strtok(".");
				if($tok2 !== false) { $tm = $tok2; }
			}

			$end_time = date("Y-m-d H:i:s", mktime($shour+$th, $smin+($tm*6), 0, date('m', $asked_date), date('d', $asked_date), date('Y', $asked_date)));

			echo "<div class='available_nc'>Available Nodes and Channels between <b>" . $start_time . "</b> and <b>" . $end_time . "</b> :</div>";
			
			echo "<table align='center'>
					<tr>
						<td>
							<form action='' method='post'>
							<input type='hidden' name='start' value='" . $start_time . "'>
							<input type='hidden' name='end' value='" . $end_time . "'>
							<input type='hidden' name='reserve' value='true'>
							<input type='submit' value='Reserve' style='background-color:#3C639F;color:white' />
						</td>
					</tr>
					</table>";
					
			echo "<div>
					<table id='reserve' cellpadding='10' align='center'>
						<tr class='top'>
							<td>Orbit/Diskless<br/>Nodes</td>
							<td>802.11a</td>
							<td>802.11b/g</td>
						</tr>
						<tr>";

			$nodes = array();
			$specs = array();
			$nodes = getFreeNodes($start_time, $end_time, $connection);
			$specs_a = getFreeSpectrum_a($start_time, $end_time, $connection);
			$specs_g = getFreeSpectrum_g($start_time, $end_time, $connection);
			$numofspecs_a = count($specs_a);
			$numofspecs_g = count($specs_g);
			$numofnodes = count($nodes);

			if ($numofnodes || $numofspecs_a || $numofspecs_g){
				echo "<td align='right'>";
				for ($i=0; $i<$numofnodes; $i++){
					$node_description = "<center><b>Node description:</b></center>" .
										"<b>Hostname:</b> ".$nodes[$i]['hostname']."<br>" .
										"<b>Control IP:</b> ".$nodes[$i]['control_ip']."";
					
					echo "<div onMouseOver=\"toolTip('".$node_description."', 200)\" onMouseOut=\"toolTip()\">";
					echo "Node " . $nodes[$i]['id'] . ": ";
					echo "<input type='checkbox' name='book_nodes[]' value='" . $nodes[$i]['id'] . "' />";
					echo "</div>";
				}
					
				echo "</td>";
				echo "<td align='right'>";
				
				for ($k=0; $k<$numofspecs_a; $k++){
					echo "<div>";
					echo "Channel " . $specs_a[$k] . ": ";
					echo "<input type='checkbox' name='book_specs[]' value='" . $specs_a[$k] . "' />";
					echo "</div>";
				}
				
				echo "</td>";
				echo "<td align='right'>";
				
				for ($l=0; $l<$numofspecs_g; $l++){
					echo "<div>";
					echo "Channel " . $specs_g[$l] . ": ";
					echo "<input type='checkbox' name='book_specs[]' value='" . $specs_g[$l] . "' />";
					echo "</div>";
				}
				
				echo "</td></tr>";
			}
				echo "</tr>";
			echo "</table></div>";

			echo "</form>";
		}
	?>
</body>
